﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sami_SMS_CRM_Business;

namespace Sami_SMS_CRM_UI
{
    public partial class crm_log :BasePage
    {
        LogRepository repository = new LogRepository();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataTable();
            pagingtop.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
                pagingtop.CurrentPage = 1;
                pagingtop.PageSize = PageSize;
                pagingtop.PageDisplay = PageDisPlay;
                pagingbottom.CurrentPage = 1;
                pagingbottom.PageSize = PageSize;
                pagingbottom.PageDisplay = PageDisPlay;
                BindDataTable();
            }
        }

        private void BindDataTable()
        {
            int currentPage = pagingtop.CurrentPage;
            int pageSize = pagingtop.PageSize;
            int totalRecord = 0;

            rptTable.DataSource = repository.GetAll(currentPage, pageSize, ref totalRecord);
            rptTable.DataBind();

            pagingtop.TotalRecord = totalRecord;
            pagingtop.Visible = pagingtop.TotalPage > 1;
            pagingbottom.TotalRecord = totalRecord;
            pagingbottom.Visible = pagingtop.TotalPage > 1;
            ltrTotalRow.Text = totalRecord.ToString();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pagingtop.PageSize = pagingbottom.PageSize = int.Parse((sender as DropDownList).SelectedValue);
            BindDataTable();
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            if (IsAdmin())
            {
                repository.ClearAll();
                BindDataTable();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"You don't have permission to clear log!\");", true);
            }
        }
    }
}